<?php

namespace app\index\controller;

use app\common\model\oms\Companies as CompanyModel;
use app\common\model\jsm\Mps as MpsModel;
use app\common\model\oms\Brands as BrandsModel;
use think\Validate;
class Company extends \app\common\controller\AuthBase {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 通过companyCode获取companyId
     */
    public function getCompanyIdByCode() {
        $param_list = [
            'companyCode' => 'string'
        ];
        $params = $this->input();
        $this->diffParam($param_list, $params);

        $res = $this->getCompanyIdByCompanyCode($params['companyCode']);

        return $this->output($res);
    }

    /**
     * 通过companyId获取companyInfo
     */
    public function getCompanyInfoById() {
        $param_list = [
            'companyId' => 'number'
        ];
        $params = $this->input();
        $this->diffParam($param_list, $params);

        $companyModel = new CompanyModel();
        $res = $companyModel->getCompanyInfoByCondition('*', ['company_id' => $params['companyId']], true);

        return $this->output($res);
    }

    /**
     * 通过companyId获取mp_id
     */
    public function getMpIdByCompanyId() {
        $params = $this->input();

        $mpsModel = new MpsModel();
        $res = $mpsModel->getMpIdByCompanyId($params['companyId']);

        return $this->output($res);
    }

    /**
     * 通过companyCode获取sms_sign
     */
    public function getSmsSignByCompanyCode() {
        $params = $this->input();
        $param_list = [
            'company_code' => 'string'
        ];
        $this->diffParam($param_list, $params);
        $companyModel = new CompanyModel();
        $res = $companyModel->getSmsSignByCompanyCode($params['company_code']);

        return $this->output($res);
    }


    
    /*
     * 获得某个集团下的品牌列表
     * 
     * @param:$companyId(集团编号)
     * 
     * @Time:2016-4-13
     * @auther:peter
     */
    public function getBrandListByCompany(){
        
        $params = $this->input(); 
        $rule = [
            "company_id" => 'require|number|max:11',
            "page"=>'number',
            "page_size"=>'number',
        ]; 
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $BrandsModel = new BrandsModel();
        
        //如果有分页参数
        if(isset($params['page'])){
            
           $page = isset($params['page'])?$params['page']:1;
           $pageSize = isset($params['page_size'])?$params['page_size']:10;
           
           $count = $BrandsModel->getPage([], true);
           $brandsPage = $BrandsModel->getPage([], false, $page, $pageSize);
           
           $data = [
               'count'=>$count,
               'list'=>$brandsPage,
               'page'=>$page
           ];
           
           return $this->output($data, 'success');
        }
        
        $list = $BrandsModel->getMutli(['company_id'=>"{$params['company_id']}"]);   

        if(empty($list)){
             return $this->output([], '此集团下未设置品牌');
        }

        return $this->output(array_values($list), 'success');
    }
    
}
